package com.mission.schedule.activity;

import java.util.ArrayList;
import java.util.List;

import com.mission.schedule.CommonViewController.CommonSearchTagActivity;
import com.mission.schedule.CommonViewController.SearchType;
import com.mission.schedule.R;
import com.mission.schedule.adapter.SerachTagAdapter;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.TagCommandBean;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.cutimage.Log;
import com.mission.schedule.utils.ListViewForScrollView;
import com.mission.schedule.utils.SharedPrefUtil;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TagSerachActivity extends BaseActivity implements OnClickListener {

    @ViewResId(id = R.id.listview)
    private ListViewForScrollView listview;
    @ViewResId(id = R.id.top_ll_back)
    private LinearLayout top_ll_back;
    @ViewResId(id = R.id.headtitle_tv)
    private TextView headtitle_tv;
    @ViewResId(id = R.id.sousuocontent_et)
    private EditText sousuocontent_et;
    @ViewResId(id = R.id.sousuo_iv)
    private ImageView sousuo_iv;

    @ViewResId(id = R.id.tv1)
    private TextView tv1;
    @ViewResId(id = R.id.tv2)
    private TextView tv2;
    @ViewResId(id = R.id.tv3)
    private TextView tv3;
    @ViewResId(id = R.id.tv4)
    private TextView tv4;
    @ViewResId(id = R.id.tv5)
    private TextView tv5;
    @ViewResId(id = R.id.tv6)
    private TextView tv6;
    @ViewResId(id = R.id.tv7)
    private TextView tv7;
    @ViewResId(id = R.id.tv8)
    private TextView tv8;
    @ViewResId(id = R.id.tv9)
    private TextView tv9;

    Context context;
    SerachTagAdapter adapter = null;
    List<TagCommandBean> mList = new ArrayList<TagCommandBean>();
    App app = App.getDBcApplication();

    @Override
    protected void setListener() {
        top_ll_back.setOnClickListener(this);
        sousuo_iv.setOnClickListener(this);

        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        tv5.setOnClickListener(this);
        tv6.setOnClickListener(this);
        tv7.setOnClickListener(this);
        tv8.setOnClickListener(this);
        tv9.setOnClickListener(this);
    }
    SharedPrefUtil sharedPrefUtil;
    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_tagserach);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        context = this;
        headtitle_tv.setText("分类查询");
        sharedPrefUtil = new SharedPrefUtil(getApplication(), ShareFile.USERFILE);
        loadData();
    }

    private void loadData() {
        mList.clear();
//        mList.addAll(app.QueryTagData(-3));
        if (sharedPrefUtil.getString(getApplication(), ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")){
            mList.addAll(app.QueryTagData(-4));
        }else{
            mList.addAll(app.QueryTagData(-5));
        }
//        TagCommandBean map = new TagCommandBean();
//        map.setCtgText("重要");
//        map.setCtgId("0");
//        mList.add(0, map);
//        TagCommandBean map2 = new TagCommandBean();
//        map2.setCtgText("到期日");
//        map2.setCtgId("0");
//        mList.add(map2);
//        TagCommandBean map1 = new TagCommandBean();
//        map1.setCtgText("收到的任务");
//        map1.setCtgId("0");
//        mList.add(map1);
//        adapter = new SerachTagAdapter(context, mList, R.layout.adapter_serachtag);
//        listview.setAdapter(adapter);
    }

    @Override
    protected void setAdapter() {
//        listview.setOnItemClickListener(new OnItemClickListener() {
//
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view,
//                                    int position, long id) {
//
//                TagCommandBean item = (TagCommandBean) listview.getAdapter().getItem(position);
//                Intent intent = null;
//                if (item.getCtgText().equals("重要")) {
//                    intent = new Intent(context, ImportantSchActivity.class);
//                } else if (item.getCtgText().equals("收到的任务")) {
//                    intent = new Intent(context, ComeFriendSchActivity.class);
//                } else if (item.getCtgText().equals("到期日")) {
//                    intent = new Intent(context, DaoqiriSchActivity.class);
//                } else {
//                    intent = new Intent(context, TagSchSerachActivity.class);
//                    intent.putExtra("tagname", item.getCtgText());
//                    intent.putExtra("tagId", item.getCtgId());
//                }
//                startActivityForResult(intent, 100);
//            }
//        });
    }

    private int ctgId(String name){
        int Id = 0;

        if (mList.size() > 0){
            for (TagCommandBean bean : mList){
                if (bean.getCtgText().equals(name)){
                    Id = Integer.valueOf(bean.getCtgId());
                    break;
                }
            }
        }
        return Id;
    }
    private int ctgDes(String name){
        int Id = 0;

        if (mList.size() > 0){
            for (TagCommandBean bean : mList){
                if (bean.getCtgText().equals(name)){
                    Id = Integer.valueOf(bean.getCtgDesc());
                    break;
                }
            }
        }
        return Id;
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.sousuo_iv:
                intent = new Intent(context, MySchSerachActivity.class);
                intent.putExtra("content", sousuocontent_et.getText().toString());
                startActivityForResult(intent, 100);
                break;
            case R.id.top_ll_back:
                this.finish();
                break;
            case R.id.tv1:
                intent = new Intent(context, YiQianActivity.class);
//                intent.putExtra("search_title","以前");
//                intent.putExtra("search_type", SearchType.BEFORE.ordinal());
                startActivityForResult(intent, 100);
                break;
            case R.id.tv2:
                intent = new Intent(context, CommonSearchTagActivity.class);
                intent.putExtra("search_title","重要");
                intent.putExtra("search_type", SearchType.IMPORT.ordinal());
                startActivityForResult(intent, 100);
                break;
            case R.id.tv3:
                intent = new Intent(context, CommonSearchTagActivity.class);
                intent.putExtra("search_title","待办事项");
                intent.putExtra("search_type", SearchType.DAIBAN.ordinal());
                startActivityForResult(intent, 100);
                break;
            case R.id.tv4:
                intent = new Intent(context, CommonSearchTagActivity.class);
                intent.putExtra("search_title", "理财");
                intent.putExtra("search_type", ctgId("理财") == 0 ? -3 : ctgId("理财"));
                intent.putExtra("search_desc", ctgDes("理财") == 0 ? 4 : ctgDes("理财"));
                startActivityForResult(intent, 100);
                break;
            case R.id.tv5:
                intent = new Intent(context, CommonSearchTagActivity.class);
                intent.putExtra("search_title", "生日");
                intent.putExtra("search_type", ctgId("生日") == 0 ? 101 : ctgId("生日") == 0);
                intent.putExtra("search_desc", ctgDes("生日") == 0 ? 1 : ctgDes("生日"));
                startActivityForResult(intent, 100);
                break;
            case R.id.tv6:
                intent = new Intent(context, CommonSearchTagActivity.class);
                intent.putExtra("search_title","收到的任务");
                intent.putExtra("search_type", SearchType.RENWU.ordinal());
                startActivityForResult(intent, 100);
                break;
            case R.id.tv7:
                intent = new Intent(context, CommonSearchTagActivity.class);
                intent.putExtra("search_title", "工作");
                intent.putExtra("search_type", ctgId("工作") == 0 ? -2 : ctgId("工作"));
                intent.putExtra("search_desc", ctgDes("工作") == 0 ? 3 : ctgDes("工作"));
                startActivityForResult(intent, 100);
                break;
            case R.id.tv8:
                intent = new Intent(context, CommonSearchTagActivity.class);
                intent.putExtra("search_title", "生活");
                intent.putExtra("search_type", ctgId("生活") == 0 ? -1 : ctgId("生活"));
                intent.putExtra("search_desc", ctgDes("生活") == 0 ? 2 : ctgDes("生活"));
                startActivityForResult(intent, 100);
                break;
            case R.id.tv9:
                intent = new Intent(context, CommonSearchTagActivity.class);
                intent.putExtra("search_title", "到期日");
                intent.putExtra("search_type", SearchType.DAOQIRI.ordinal());
                startActivityForResult(intent, 100);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        super.onActivityResult(arg0, arg1, arg2);

    }

}
